package com.linln.modules.system.service.impl;

import com.linln.common.enums.StatusEnum;
import com.linln.common.jpahelper.Criteria;
import com.linln.modules.system.domain.Upload;
import com.linln.modules.system.repository.UploadRepository;
import com.linln.modules.system.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author 小懒虫
 * @date 2018/11/02
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UploadRepository uploadRepository;

    /**
     * 获取文件sha1值的记录
     */
    @Override
    public Upload getBySha1(String sha1) {
        return uploadRepository.findBySha1(sha1);
    }

    /**
     * 保存文件上传
     * @param upload 文件上传实体类
     */
    @Override
    public Upload save(Upload upload){
        return uploadRepository.save(upload);
    }

    @Override
    public Upload getById(Long id) {
        return uploadRepository.findById(id).orElse(null);
    }

    @Override
    public List<Upload> search(Criteria<Upload> criteria) {
        return uploadRepository.findAll(criteria);
    }

    @Override
    public void deleteById(Long id) {
        uploadRepository.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        uploadRepository.deleteByIdIn(ids);
    }

    @Override
    public List<Upload> findByIds(List<Long> ids) {
        return uploadRepository.findAllById(ids);
    }
    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return uploadRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

}

